//
//  ZHomeTableViewController.swift
//  CZWeibo
//
//  Created by 周振兰 on 16/5/9.
//  Copyright © 2016年 周振兰. All rights reserved.
//

import UIKit

class ZHomeTableViewController: CZBaseTableViewController {
    let identifier = "cell"
    //显示微薄数据
    var statuses:[CZStatus]?
        {
        didSet
        {
            //模型数据改变了
            self.tableView.reloadData()
        }
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        //没有登录就不要执行后面的代码
        if !CZUserAccountViewModel.shareIntance.isUserLogin
        {
            return
        }
        
     setupNavigationBar()
        //注册cell
        tableView.registerClass(CZStatusCell.self, forCellReuseIdentifier: identifier)
        
        //设置cell的预估行高
        tableView.estimatedRowHeight = 200
        // 根据cell的约束自动计算行高, 测量contentView的高度
        tableView.rowHeight = UITableViewAutomaticDimension
        //取消分隔线
        tableView.separatorStyle = UITableViewCellSeparatorStyle.None
        
    //加载微薄数据
        CZStatusViewModel.shardIntance.loadStatus { (statuses, error) -> () in
            if error != nil
            {
                CZPrint(items: "请求微博数据出错了\(error)")
                return
            }
            //请求成功了
            CZPrint(items: "在控制器里打印数据\(statuses)")
            self.statuses = statuses
        }
        //添加下拉刷新控件到tableview
        tableView.addSubview(pullDownToRefreshControl)
    }
   
//设置导航栏
   private  func setupNavigationBar()
    {
       navigationItem.leftBarButtonItem = UIBarButtonItem(imageName: "navigationbar_friendsearch", target: self, action: "didClickFriendsearch")
        navigationItem.rightBarButtonItem = UIBarButtonItem(imageName: "navigationbar_pop", target: self, action: "didClickPop")
        
        //设置首页titleView
        let button = CZHomeTitleButton()
        //获取用户名称
        let name = CZUserAccountViewModel.shareIntance.userAccount?.screen_name ?? "未知名称"
        button.setTitle(name, forState: UIControlState.Normal)
        button.setImage(UIImage(named: "navigationbar_arrow_down"), forState: UIControlState.Normal)
        button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        button.addTarget(self, action: "didClickTitleView:", forControlEvents: UIControlEvents.TouchUpInside)
        button.sizeToFit()
        navigationItem.titleView = button
    }
    //MARK:- 按钮点击事件
    func didClickTitleView(button:UIButton)
    {
        //button有一个selected的选中属性 如果是选中状态则取反
        button.selected = !button.selected
        UIView.animateWithDuration(0.3) { () -> Void in
          //  1.哪边近走那边
           // 2.2边一样就顺时针
            //三元运算符，
            let trasfram = button.selected ? CGAffineTransformMakeRotation(CGFloat(M_PI - 0.01)):CGAffineTransformIdentity
            button.imageView?.transform = trasfram
        }
    }
    
    func didClickFriendsearch()
    {
        CZPrint(items: "点击左边")
        let VM = CZUserAccountViewModel.shareIntance
        print("VM\(VM)")
    }
    func didClickPop()
    {
        CZPrint(items: "点击右边")
    }
    
    //MARK： - 懒加载
    private lazy var pullDownToRefreshControl:CZPullDownToRefreshControl = CZPullDownToRefreshControl()
}


//显示tableView的数据
extension ZHomeTableViewController
{
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        //前面的可选有值就是用，没值就用后面的值
        return statuses?.count ?? 0
    }
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        //根据identity去缓池中查找，缓池中没有，就有注册的cell
        let cell = tableView.dequeueReusableCellWithIdentifier(identifier, forIndexPath: indexPath) as! CZStatusCell
        //获得相对应的模型
        let status = self.statuses![indexPath.row]
       //1.数据源给cell设置对应的模型
        cell.status = status
        return cell
        
    }
}

